﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using WindowsInput.Native;
using System.Collections.ObjectModel;

namespace UniPie
{
	/// <summary>
	/// This control is used for recording and storing key/mouse combos.
	/// </summary>
	public partial class KeyboardMousePicker : UserControl , INotifyPropertyChanged
	{
		public KeyboardMousePicker()
		{
			InitializeComponent();
		}

//##################################################################
#region Properties

		/// <summary>
		/// KeyboardMouseCombo property.
		/// 
		/// Stores a series of VirtualKeyCodes.
		/// </summary>

		[Category("Common")]
		public ObservableCollection<VirtualKeyCode> KeyboardMouseCombo
		{
			get { return (ObservableCollection<VirtualKeyCode>)this.GetValue(KeyboardMouseComboProperty); }
			set
			{
				if (value != this.KeyboardMouseCombo)
				{
					this.SetValue(KeyboardMouseComboProperty, value);
					NotifyPropertyChanged("KeyboardMouseCombo");
				}
			}
		}

		public static readonly DependencyProperty KeyboardMouseComboProperty = DependencyProperty.Register(
			"KeyboardMouseCombo",
			typeof(ObservableCollection<VirtualKeyCode>),
			typeof(KeyboardMousePicker),
			new UIPropertyMetadata(KeyboardMousePicker.KeyboardMouseComboChanged));

		private static void KeyboardMouseComboChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{

		}

		/// <summary>
		/// IsRecording property.
		/// 
		/// Defines if the control is in recording mode.
		/// </summary>

		[Category("Common")]
		public bool IsRecording
		{
			get { return (bool)this.GetValue(IsRecordingProperty); }
			set
			{
				if (value != this.IsRecording)
				{
					this.SetValue(IsRecordingProperty, value);
					NotifyPropertyChanged("IsRecording");
				}
			}
		}

		public static readonly DependencyProperty IsRecordingProperty = DependencyProperty.Register(
			"IsRecording",
			typeof(bool),
			typeof(KeyboardMousePicker),
			new UIPropertyMetadata(KeyboardMousePicker.IsRecordingChanged));

		private static void IsRecordingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
		}

#endregion

//##################################################################
#region Event handlers

		/// <summary>
		/// 
		/// </summary>
		public event PropertyChangedEventHandler PropertyChanged;

		/// <summary>
		/// Notifies the property changed.
		/// </summary>
		/// <param name="property">The property.</param>
		private void NotifyPropertyChanged(string property)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(property));
			}
		}

		/// <summary>
		/// Activates recording.
		/// </summary>
		private void ActivateRecording(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			if (!IsRecording)
			{
				IsRecording = true;
				VisualStateManager.GoToState(this, "Recording", false);
			}
		}

		/// <summary>
		/// Deactivates recording.
		/// </summary>
		private void DeactivateRecording(object sender, System.Windows.Input.MouseEventArgs e)
		{
			if (IsRecording)
			{
				IsRecording = false;
				VisualStateManager.GoToState(this, "Default", false);
			}
		}

		/// <summary>
		/// Clears the stored combo.
		/// </summary>
		private void ClearCombo(object sender, MouseButtonEventArgs e)
		{
			KeyboardMouseCombo.Clear();
		}
#endregion

	}
}